<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/12/012
 * Time: 10:26
 */

namespace app\project\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

class AccountPro extends Controller
{
    //关联所有项目
    public function accountAll(){
        try{
            $thisWechat = Session::get("thisWechat");
            $where['uniacid'] = $thisWechat['id'];
            $where['status'] = 1;

            $library = $_POST['library'];
            if(!empty($library)){
                $where['library'] = $library;
            }

            if(!empty($_POST['sign'])){
                $where['sign'] = $_POST['sign'];
            }

            $prolist = Db::name("project_pro")->where($where)->field("id")->select();
            $prolist = array_column($prolist,"id");

            $type = $_POST['type'];

            if($type==1){
                $account_id = $_POST['account_id'];
                foreach ($prolist as $v){
                    $data = [];
                    $data['account_id'] = $account_id;
                    $data['project_id'] = $v;
                    $id = Db::name("project_account_pro")->where($data)->value("id");
                    if(empty($id)){
                        $data['addtime'] = date("Y-m-d H:i:s", time());
                        $data['add_id'] = Session::get("id");
                        Db::name("project_account_pro")->insertGetId($data);
                    }
                }
            }else{
                $data['account_id'] = $_POST['account_id'];
                $data['project_id'] = ['in',$prolist];
                Db::name("project_account_pro")->where($data)->delete();
            }

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 关联项目操作
     */
    public function accountpro()
    {
        try {
            $data['account_id'] = $_POST['account_id'];
            $data['project_id'] = $_POST['project_id'];

            //判断是否已关联
            $id = Db::name("project_account_pro")->where($data)->value("id");
            if (!empty($id)) {
                throw new Exception("请勿重复关联项目");
            }

            $data['addtime'] = date("Y-m-d H:i:s", time());
            $data['add_id'] = Session::get("id");
            $id = Db::name("project_account_pro")->insertGetId($data);

            //查询用户名称
            $name = Db::name("project_account")->where(['id' => $_POST['account_id']])->value("name");
            $title = Db::name("project_pro")->where(['id' => $_POST['project_id']])->value("title");

            $common = new Common();
            $log['remark'] = "【工程管理】用户【" . Session::get("user_name") . "】为前端用户【" . $name . "】关联新项目【" . $title . "】,项目id【" . $id . "】";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 解绑项目
     * @return mixed
     */
    public function unbundling()
    {
        try {
            $data['account_id'] = $_POST['account_id'];
            $data['project_id'] = $_POST['project_id'];
            Db::name("project_account_pro")->where($data)->delete();
            //查询用户名称
            $name = Db::name("project_account")->where(['id' => $_POST['account_id']])->value("name");
            $title = Db::name("project_pro")->where(['id' => $_POST['project_id']])->value("title");
            $common = new Common();
            $log['remark'] = "【工程管理】用户【" . Session::get("user_name") . "】解绑前端用户【" . $name . "】所关联的项目【" . $title . "】,项目id【" . $_POST['project_id'] . "】";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}